package cn.famouscat.ex002;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.*;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.concurrent.*;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class MainActivity extends ActionBarActivity {

    private static final String TAG = "MainActivity";

    Button mBtnA;
    Button mBtnB;
    Button mBtnC;

    MyThread thread;
    MyThread thread2;

    LinkedBlockingDeque<Runnable> linkedBlockingDeque = new LinkedBlockingDeque<>(5);
    ThreadFactory threadFactory = new ThreadFactory() {
        int i = 0;
        @Override
        public Thread newThread(Runnable r) {
            i++;
            Log.d(TAG,"=========new thread "+ i);
            return new Thread(r,"thread-"+i);
        }
    };

    ThreadPoolExecutor executor = new ThreadPoolExecutor(2,5,20, TimeUnit.MILLISECONDS,linkedBlockingDeque,threadFactory);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnA = (Button) findViewById(R.id.btn_a);
        mBtnB = (Button) findViewById(R.id.btn_b);
        mBtnC = (Button) findViewById(R.id.btn_c);

        thread = new MyThread();
        thread.start();

        thread2 = new MyThread();
        thread2.start();
    }




    public void onBtnClick(View v){
        switch (v.getId()){
            case R.id.btn_a:{
                Message message = thread.handler.obtainMessage(1);
                message.sendToTarget();
            }break;
            case R.id.btn_b:{
                Message message = thread2.handler.obtainMessage(2);
                message.sendToTarget();
            }break;
            case R.id.btn_c:{
                Message message = thread2.handler.obtainMessage(22);
                message.sendToTarget();
            }break;
            case R.id.btn_d:{
                Intent intent = new Intent();
                intent.setClass(this,MyIntentService.class);
                startService(intent);
            }break;
            case R.id.btn_e:{
                handlerThread();
            }break;
            case R.id.btn_f:{
                asyncTask();
            }break;
            case R.id.btn_g:{
                Thread thread = new Thread("async"){
                    @Override
                    public void run() {
                        asyncTask();
                    }
                };

                thread.start();

            }break;
            case R.id.btn_h:{
//                Executors.newFixedThreadPool(5);
                Runnable a = new Runnable() {
                    @Override
                    public void run() {

                    }
                };
                Executor executor = new Executor() {
                    @Override
                    public void execute(Runnable command) {
                        command.run();
                    }
                };
                executor.execute(a);
            }break;
            case R.id.btn_i:{

                for(int i=1;i<20;i++){

                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(20000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            Log.d(TAG,"====run finish "+Thread.currentThread().getName());
                        }
                    };
                    Log.d(TAG,"========new "+ i +" Runnable");
                    executor.execute(runnable);
                    Log.d(TAG,"========execute "+ i +" Runnable");
                }

            }break;
            case R.id.btn_j:{
                Intent intent = new Intent();
                intent.setClass(this,AActivity.class);
                startActivity(intent);
            }break;
        }
    }


    private void asyncTask(){
        new AsyncTask<Integer,Integer,Integer>(){
            @Override
            protected Integer doInBackground(Integer... params) {

                return null;
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Integer integer) {
                super.onPostExecute(integer);
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
            }

            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            protected void onCancelled(Integer integer) {
                super.onCancelled(integer);
            }

            @Override
            protected void onCancelled() {
                super.onCancelled();
            }
        }.execute(1);
    }

    private void handlerThread(){
        HandlerThread thread = new HandlerThread("myHandlerThread");
        thread.start();
        MyHandler handler = new MyHandler(thread.getLooper());
        handler.obtainMessage(33).sendToTarget();
    }


    private class MyHandler extends Handler{
        public MyHandler(Looper looper){
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.d(TAG,"=======msg:"+msg.what+":"+Thread.currentThread().getName());
        }
    }

    private class MyThread extends Thread{
        public MyHandler handler;

        @Override
        public void run() {
            super.run();
            Looper.prepare();
            handler = new MyHandler(Looper.myLooper());
            Looper.loop();
        }
    }


}
